define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'businessmanage/select/index?bid='+ Config.bid,
                    del_url: 'businessmanage/select/del?bid='+ Config.bid,
                }
            });

            var table = $("#table");

            //在表格内容渲染完成后回调的事件
            table.on('post-body.bs.table', function (e, json) {
                $("tbody tr[data-index]", this).each(function () {
                    if (parseInt($("td:eq(1)", this).text()) == Config.admin.id) {
                        $("input[type=checkbox]", this).prop("disabled", true);
                    }
                });
            });

            // 初始化表格
            table.bootstrapTable({
                // searchFormVisible:true,
                // search:false,
                // showSearch: false,
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        // {field: 'id', title: 'ID',operate: false},
                        {field: 'admindate.username', title: __('Username') },
                        {field: 'admindate.nickname', title: __('Nickname')},
                        {field: 'admindate.point', title: '商机点',cellStyle: function (value, row, index) { return {css: {"color": "red"}};}},
                        {field: 'admindate.email', title: __('Email')},
                        {field: 'admindate.mobile', title: __('Mobile')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        pushuser: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'businessmanage/select/pushuser?bid='+ Config.bid,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: 'ID',operate: false,},
                        {field: 'username', title: __('Username') },
                        {field: 'nickname', title: __('Nickname')},
                        {field: 'email', title: __('Email')},
                        {field: 'mobile', title: __('Mobile')},
                        {field: 'operate', 
                            title: __('Operate'), table: table, 
                            events: Table.api.events.operate, 
                            formatter: Table.api.formatter.operate,
                            buttons: [{
                                name: 'select',
                                text: '添加推送',
                                icon: 'fa fa-plus',
                                classname: 'btn btn-success btn-xs btn-add btn-ajax',
                                url: 'businessmanage/select/addpush?bid='+ Config.bid,
                                success: function () {
                                    $("a.btn-refresh").trigger("click");
                                    parent.$("a.btn-refresh").trigger("click");
                                },
                            },],
                        }
                    ]
                ]
            });
            $("#allpush").on("click",function(){
                var ids = Table.api.selectedids(table);
                Fast.api.ajax({
                    type: 'POST',
                    url: 'businessmanage/select/addpush',
                    data: {'ids':ids,'bid':Config.bid},
                    dataType: "json"
                }, function (data, ret) {
                    $("a.btn-refresh").trigger("click");
                    parent.$("a.btn-refresh").trigger("click");
                }, function (data, ret) {
                    console.error(data);
                    return false;
                });
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        
        add: function () {
            Form.api.bindevent($("form[role=form]"));
        },
        edit: function () {
            Form.api.bindevent($("form[role=form]"));
        },
        addpush: function () {
            Controller.api.bindevent();
        },
    };
    return Controller;
});
